package com.mercenary.wein.screenControl.ui.batch;

import com.mercenary.wein.screenControl.bean.ExeModeBean;
import com.mercenary.wein.screenControl.ui.util.DateChooserJButton;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by tuxiao on 2017/3/24.
 */
public class ExeModeButtonGroup extends JPanel {

    private final ExeModeBean exeModeBean = new ExeModeBean();
    JTextField taskName = new JTextField();
    DateChooserJButton taskTime ;
    JComboBox comboBox = new JComboBox();

    public ExeModeBean getExeModeBean() {
        exeModeBean.setJobName(taskName.getText());
        exeModeBean.setExeTime(taskTime.getText());
        exeModeBean.setTimeUnit(comboBox.getSelectedIndex());
        return exeModeBean;
    }

    public ExeModeButtonGroup(JDialog owner){
        super(new BorderLayout());
        final JPanel jptime = new JPanel();
        JPanel jpSet = new JPanel(new BorderLayout());
        JRadioButton jrb1 = new JRadioButton("立即执行");
        JRadioButton jrb2 = new JRadioButton("定时执行");
        JRadioButton jrb3 = new JRadioButton("循环执行");
        taskTime = new DateChooserJButton(owner);
        jpSet.add(jrb1, BorderLayout.NORTH);
        jpSet.add(jrb2, BorderLayout.CENTER);
        jpSet.add(jrb3, BorderLayout.SOUTH);

        add(jpSet,BorderLayout.NORTH);

        ButtonGroup excutegroup = new ButtonGroup();// 创建单选按钮组
        excutegroup.add(jrb1);// 将radioButton1增加到单选按钮组中
        excutegroup.add(jrb2);// 将radioButton2增加到单选按钮组中
        excutegroup.add(jrb3);// 将radioButton2增加到单选按钮组中
        jrb1.setSelected(true);
        exeModeBean.setMode(0);
        //这个是提交任务的三个选项

        final JLabel jl1 = new JLabel("任务名称");
        final JLabel jl2 = new JLabel("执行时间");
        taskName.setPreferredSize(new Dimension(100, 25));
        taskTime.setPreferredSize(new Dimension(140, 25));

        jrb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jptime.removeAll();
                jptime.repaint();
                exeModeBean.setMode(0);
                exeModeBean.setJobName("立即执行任务");
            }
        });
        jrb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jptime.removeAll();
                jptime.repaint();
                jptime.add(jl1);
                jptime.add(taskName);
                jptime.add(jl2);
                jptime.add(taskTime);
                add(jptime, BorderLayout.SOUTH);
                exeModeBean.setMode(1);

                validate();
            }
        });

        jrb3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jptime.removeAll();
                jptime.repaint();
                JTextField cycleTimes = new JTextField();
                cycleTimes.setPreferredSize(new Dimension(50, 25));
                jptime.setLayout(new FlowLayout(FlowLayout.LEADING));
                String[] groups = {"分钟", "小时", "天"};//所有的分组的组的集合
                for (int k = 0; k < groups.length; k++) {
                    comboBox.addItem(groups[k]);
                }
                jptime.add(jl1);
                jptime.add(taskName);
                jptime.add(jl2);
                jptime.add(taskTime);
                jptime.add(cycleTimes);
                jptime.add(comboBox);
                add(jptime, BorderLayout.SOUTH);
                exeModeBean.setMode(2);
                exeModeBean.setJobName(taskName.getText());
                exeModeBean.setExeTime(taskTime.getText());
                exeModeBean.setInterval(cycleTimes.getText().matches("\\d+")?Integer.parseInt(cycleTimes.getText()):1);
                validate();
            }
        });


    }
}
